dojo.provide("lotex.string");


dojo.require("dojo.validate.common");


lotex.string.Null = null;

lotex.string = {
    isBlank: function(/*String*/str) {
        return (typeof str == "undefined" || str==null || str=="") ? true : false;
    },

    splice: function(str, idx, len, rpstr) {
        return str.substring(0, idx)
            + rpstr
            + str.substring(idx+len, str.length);
    },

    multiply: function(str,len) {
        var tmp = [];
        for(var i=0;i<len;i++) {
            tmp.push(str);
        }
        return tmp.join("");
    },

    str2reg: function(str, arg){
        if(arg) return new RegExp(str, arg);
        return new RegExp(str);
    },

    substr_count: function(haystack, needle){
        // TODO: fix keywords in RegExp
        var reg = this.str2reg(needle, "g");
        var r = haystack.match(reg);
        if (r==null) return 0;
        return r.length;
    },

    replace: function(subject, search, replace) {
        var reg = this.str2reg(search, "g");
        var r = subject.replace(reg, replace);
        return r;
    },

    sprintf: function(str, data) {
        var re = /\$\{(\w+)\}/g;
        return (str.replace(
                    re,
                    function($0,$1) {
                        if(dj_undef($1,data)) return $0;
                        return (data[$1]);
                    })
                );
    },

    cleanComments: function(str) {
        var commentsRe = [/(\/\*)\/?(([^\*]\/)|[^\/])*(\*\/)/g,
            /(\s|^)+\/\/([^\n\r])*/g];
        str = str.replace(commentsRe[0], "");
        str = str.replace(commentsRe[1], "");

        return str;
    }
}

lotex.string.unescapeXml = function(str) {
    str = str.replace(/&amp;/gm, "&").replace(/&lt;/gm, "<")
        .replace(/&gt;/gm, ">").replace(/&quot;/gm, "\"")
        .replace(/&#39;/gm, "'");
    return str;
}
